package com.miao.hong.fang.teacher;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.miao.hong.fang.IConstants;
import com.miao.hong.fang.course.service.CourseService;
import com.miao.hong.fang.teacher.service.TeacherService;
import com.miao.hong.fang.util.SpringBeansHolder;
import com.miao.hong.fang.util.Utils;
import com.miao.hong.fang.util.pagination.ListAndCount;
import com.miao.hong.fang.util.pagination.PaginationContext;

public class TeacherAction extends DispatchAction{

	private TeacherService teacherService = SpringBeansHolder.getInstance().getTeacherService();
	private CourseService courseService = SpringBeansHolder.getInstance().getCourseService();
	
	public ActionForward viewTeacher(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) {
		
		String teacherId = request.getParameter("teacherId");
		TeacherTO to = teacherService.getTeacherDetail(teacherId);
		String teacherImagePath = request.getContextPath()+ "/images/teacher/" + to.getTeacherImageName();
		
		request.setAttribute("teacher", to);
		request.setAttribute("imagePath", teacherImagePath);
		
		List courseCategory = Utils.getCellOfList(courseService.getAllCourseCategory(), 6, IConstants.ORDER_TYPE_FRONT);
		request.setAttribute("courseCategory", courseCategory);
		return mapping.findForward("success");
	}
	
	public ActionForward viewAllTeacher(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws UnsupportedEncodingException {
		
		String category = Utils.parseZhCN(request.getParameter("category"));
		ListAndCount result = teacherService.getAllTeacher(category);
		
		List teacher = result.getList();
		TeacherTO to = null;
		for (int i = 0; i < teacher.size(); i++) {
			 to = (TeacherTO) teacher.get(i);
			 String teacherImagePath = request.getContextPath() + "/images/teacher/" + to.getTeacherImageName();
			 to.setTeacherImagePath(teacherImagePath);
		}
		
		request.setAttribute("teacherList", teacher);
		request.setAttribute("totalCount", String.valueOf(result.getCount()));
		request.setAttribute("category", category);
		request.setAttribute("paginationSortOrderData", PaginationContext.getPaginationSortOrderData());

		List courseCategory = Utils.getCellOfList(courseService.getAllCourseCategory(), 6, IConstants.ORDER_TYPE_FRONT);
		request.setAttribute("courseCategory", courseCategory);
		return mapping.findForward("success");
	}
}
